package beans;


import java.sql.*;

import javax.naming.NamingException;
import tools.*;

public class ProductBean {
	private int id = 0;
	private String name = "";
	private int category = 0;
	private double price = 0;
	private String image = null;
	private String description= "";
	

	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	
	public ProductBean()
	{}
	
	public int getID()
	{
		return id;
	}
	
	public void setId(int id)	
	{
		this.id=id;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public int getCategory() {
		return category;
	}

	public void setCategory(int category) {
		this.category = category;
	}
	
	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}
	
	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}
	
	public String getDesc() {
		return description;
	}

	public void setDesc(String description) {
		this.description = description;
	}
	
	public boolean setProductByID(){
		if(id == 0)
			return false;
		try{
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format("SELECT * FROM t_product WHERE t_product.id = %d ", id);
			rs = stmt.executeQuery(sqlCmd);
			
			rs.last();
			if(rs.getRow() != 1){
				return false;
			} else {
				setName(rs.getString("Name"));
				setCategory(rs.getInt("Category"));
				setPrice(rs.getDouble("Price"));
				setImage(rs.getString("Image"));
				setDesc(rs.getString("Description"));
			}
			
		} catch (SQLException sqlException){
			
		} catch (NamingException namingException){
			
		} finally{
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		return true;
	}
	
	public boolean updateProductByID(){
		if(id == 0)
			return false;
		try{
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format("SELECT * FROM t_product WHERE t_product.id = %d ", id);
			rs = stmt.executeQuery(sqlCmd);
			
			rs.last();
			if(rs.getRow() != 1){
				return false;
			} else {
				//rs.updateInt("Category",category);				
				rs.updateDouble("Price", price);
			    //rs.updateString("Image",image);
				//rs.updateString("Description",description);
				rs.updateRow();
			}
			
		} catch (SQLException sqlException){
			
		} catch (NamingException namingException){
			
		} finally{
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		return true;
	}
	
	public boolean insertProduct(){
		try{
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format("SELECT * FROM t_product");
			rs = stmt.executeQuery(sqlCmd);
			
			rs.last();
			rs.moveToInsertRow();
			
			rs.updateString("Name", name);
			rs.updateInt("Category",category);				
			rs.updateDouble("Price", price);
		    rs.updateString("Image",image);
			rs.updateString("Description",description);
			
			rs.insertRow();
			
			
		} catch (SQLException sqlException){
			
		} catch (NamingException namingException){
			
		} finally{
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		return true;
	}
	
	public boolean maxProductID()
	{
		try{
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format("SELECT max(id) as maxid FROM t_product ");
			rs = stmt.executeQuery(sqlCmd);
			
			rs.last();
			if(rs.getRow() != 1){
				return false;
			} else 
			{
				int id = rs.getInt("maxid");
				this.id=id;
			}
		} catch (SQLException sqlException){
			
		} catch (NamingException namingException){
			
		} finally{
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		return true;
	}

}
